<?php
class SocialUtility {
	// call constructor
	private function __construct() {}

	
	/**
	 * Save user's linkedin social data
	 * @param $postVars 
	 * true/false
	*/
	public static function saveUserLinkedinData( $postVars ){
		try{
			global $eiLoginUserId;
			if( $eiLoginUserId <= 0 ){
				throw new Exception( 'Your login session expired, Please login first!' );
			}
			$socialData = $postVars[ 'socialData' ];
			$socialId = $socialData[ 'id' ];
			$createdUserId = $postVars[ 'userId' ];
			$friends = $postVars[ 'friends' ];
			if( $createdUserId <= 0 || $socialData == '' || $socialId == '' || $friends == '' ){
				return false;
			}
			// check for record existance for user's linkedin social data
			$linkedinUserObj = getMultiple( 'userSocial', 'type||linkedin', 
				"createdUserId||$createdUserId", "socialId||$socialId" );
			if( $linkedinUserObj->getResultCount() > 0 ) {
				// edit user's linkedin data
				$linkedinUserObj->get( 0 )->update( 'data||' . serialize( $socialData ), 
					'friendData||' . serialize( $friends ) );
				return true;
			} else {
				// add user's linkedin data
				getDb( 'userSocial', 'add' )->set( 'type||linkedin', 
					"socialId||$socialId", 'data||'.serialize( $socialData ),
					'friendData||'.serialize( $friends ), "createdUserId||$createdUserId"
				);
				return true;
			}
			return false;
		}catch( Exception $ex ){
			throw $ex;
		}
	}


	/**
	 * Save user's facebook social data
	 * @param $postVars 
	 * true/false
	*/
	public static function saveUserFacebookData( $postVars ){
		try{
			global $eiLoginUserId;
			if( $eiLoginUserId <= 0 ){
				throw new Exception( 'Your login session expired, Please login first!' );
			}
			$socialData = $postVars[ 'socialData' ];
			$socialId = $socialData[ 'id' ];
			$createdUserId = $postVars[ 'userId' ];
			$friends = $postVars[ 'friends' ];
			if( $createdUserId <= 0 || $socialData == '' || $socialId == '' || $friends == '' ){
				return false;
			}
			// check for record existance for user's facebook social data
			$fbUserObj = getMultiple( 'userSocial', 'type||facebook', 
				"createdUserId||$createdUserId", "socialId||$socialId" );
			if( $fbUserObj->getResultCount() > 0 ) {
				// edit user's facebook data
				$fbUserObj->get( 0 )->update( 'data||' . serialize( $socialData ), 
					'friendData||' . serialize( $friends ) );
				return true;
			} else {
				// add user's facebook data
				getDb( 'userSocial', 'add' )->set( 'type||facebook', 
					"socialId||$socialId", 'data||'.serialize( $socialData ),
					'friendData||'.serialize( $friends ), "createdUserId||$createdUserId"
				);
				return true;
			}
			return false;
		}catch( Exception $ex ){
			throw $ex;
		}
	}

}
